package com.yuan.service.fetchStrategy.xquery;

import static com.yuan.common.constant.YuanConstant.YYYYMMDD;

import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;
import org.webharvest.definition.ScraperConfiguration;
import org.webharvest.runtime.Scraper;

import com.yuan.common.date.DateUtil;
import com.yuan.goods.Goods;
import com.yuan.goods.GoodsContainer;
import com.yuan.service.fetchStrategy.FetchStrategy;

/**
 * 
 * @author Beni
 * 
 */
public class XQueryFetchStrategy implements FetchStrategy {

	private static final Logger logger = Logger.getLogger(XQueryFetchStrategy.class);

	private static ResourceBundle resourceBundle;

	static {
		resourceBundle = ResourceBundle.getBundle("service");
	}

	public List<Goods> run(String url) {

		try {
			
			logger.info("开始webharvest:" + url);
			String filePath=resourceBundle.getString("xqueryResultPath")+DateUtil.format(new Date(), YYYYMMDD)+"/";
			ScraperConfiguration config = new ScraperConfiguration(resourceBundle.getString("xqueryPath") + url + ".xml");
			Scraper scraper = new Scraper(config,filePath );

			long startTime = System.currentTimeMillis();
			scraper.execute();

			GoodsContainer goodsContainer=GoodsContainer.fromXml(filePath+url+"/0yuan.xml");
			
			logger.info("结束webharvest:" + url + ",耗时：" + (System.currentTimeMillis() - startTime) + "ms");
			return goodsContainer.getGoods();

		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error("webharvest失败，url:" + url, ex);
		}
		return null;
	}

}
